package com.quanyan.ic.vendingMachine.dao.impl;

import com.alibaba.fastjson.JSON;
import com.quanyan.ic.vendingMachine.dao.VmInventoryDAO;
import com.quanyan.ic.vendingMachine.mapper.VmInventoryStreamEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.VmReplenishmentRecordEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.customSQL.VmInventoryMapper;
import com.quanyan.ic.vendingMachine.mapper.entity.VmInventoryStreamEntity;
import com.quanyan.ic.vendingMachine.mapper.entity.VmReplenishmentRecordEntity;
import com.quanyan.ic.vendingMachine.model.InventoryStream;
import com.quanyan.ic.vendingMachine.model.ReplenishmentInfo;
import com.quanyan.ic.vendingMachine.model.ReplenishmentRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

/**
 * Created by zhuo.shi on 2017/8/3.
 */
@Repository
public class VmInventoryDAOImpl implements VmInventoryDAO {

    @Autowired
    private VmInventoryMapper vmInventoryMapper;
    @Autowired
    private VmInventoryStreamEntityMapper vmInventoryStreamEntityMapper;
    @Autowired
    private VmReplenishmentRecordEntityMapper vmReplenishmentRecordEntityMapper;

    @Override
    public boolean updateGridInventory(String vmNumber, String grid, Byte gridNumber, Integer opCount) {
        if (StringUtils.isEmpty(vmNumber) || grid == null || gridNumber == null || opCount == null) {
            return false;
        }
        int ret = vmInventoryMapper.updateGridInventory(vmNumber, grid, gridNumber, opCount);
        return (ret == 1);
    }

    @Override
    public int countVmInventoryOfGoods(String vmNumber, Integer goodsId, String goodsUuid) {
        return vmInventoryMapper.countVmInventoryOfGoods(vmNumber, goodsId, goodsUuid);
    }

    @Override
    public void insertInventoryStream(InventoryStream stream) {
        if (stream == null || stream.getReplenishmentInfo() == null) {
            return;
        }
        vmInventoryStreamEntityMapper.insertSelective(toVmInventoryStreamEntity(stream));
    }

    @Override
    public void insertReplenishmentRecord(ReplenishmentRecord record) {
        if (record == null || StringUtils.isEmpty(record.getVmNumber())) {
            return;
        }
        vmReplenishmentRecordEntityMapper.insertSelective(toVmReplenishmentRecordEntity(record));
    }

    private VmInventoryStreamEntity toVmInventoryStreamEntity(InventoryStream stream) {
        VmInventoryStreamEntity entity = new VmInventoryStreamEntity();
        entity.setVmNumber(stream.getVmNumber());
        ReplenishmentInfo rsptInfo = stream.getReplenishmentInfo();
        entity.setGrid(rsptInfo.getGrid());
        entity.setGridNumber(rsptInfo.getGridNumber());
        entity.setGoodsId(rsptInfo.getGoodsId());
        entity.setGoodsUuid(rsptInfo.getGoodsUuid());
        entity.setGoodsName(rsptInfo.getGoodsName());
        entity.setOriginalInventory(stream.getOriginalInventory());
        entity.setOpCount(stream.getOpCount());
        return entity;
    }

    private VmReplenishmentRecordEntity toVmReplenishmentRecordEntity(ReplenishmentRecord record) {
        VmReplenishmentRecordEntity entity = new VmReplenishmentRecordEntity();
        entity.setVmNumber(record.getVmNumber());
        entity.setVmName(record.getVmName());
        entity.setPlaceId(record.getPlaceId());
        entity.setPlaceName(record.getPlaceName());
        entity.setOpUser(record.getOpUser());
        entity.setOpPwd(record.getOpPwd());
        entity.setOpTime(record.getOpTime());
        entity.setSnapshootBefore(JSON.toJSONString(record.getSnapshoot_1()));
        entity.setSnapshootAfter(JSON.toJSONString(record.getSnapshoot_2()));
        entity.setSummary(JSON.toJSONString(record.getSummaryList()));
        return entity;
    }
}
